var myChar = echarts.init(document.getElementById('main'));

function echart(type, count, name) {
    var option = {
        //标题
        title: {
            text: '商品统计图',//主标题
            subtext: '思云产品'//副标题
        },
        //图例位置
        legend: {
            show: true
        },

        //右上角工具栏
        toolbox: {
            feature: {
                dataView: { //数据视图
                    show: true
                },
                magicType: {//动态类型切换
                    type: ['line', 'bar']
                },
                restore: { //重置
                    show: true
                },
                saveAsImage: {//保存图片
                    show: true
                }
            }
        },
        //提示框
        tooltip: {},
        //y轴
        yAxis: {
            type: 'value',
            position: 'left',
        },
        xAxis: {
            data: type
        },
        series: [
            {
                //名称，提示框tooltip会显示
                name: name,
                //决定这的图表是什么类型的
                type: 'bar',
                data: count,
                markPoint: {
                    data: [
                        {type: 'max', name: 'Max'},
                        {type: 'min', name: 'Min'}
                    ]
                }
            }
        ]
    }
    //使用指定的配置项和数据显示图表
    myChar.setOption(option);
}

//点击量
$('#hitsSum').on('click', function () {
    $('#title').html('');
    $('#sum').html('');
    hitsSum();
})
//收藏量
$('#collectionCount').on('click', function () {
    $('#title').html('');
    $('#sum').html('');
    collectionCount();
})

//收藏量
function collectionCount() {
    $.ajax({
        url: '/goodsTree/collectionCount',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            var type = new Array();
            var count = new Array();
            var lists = data.data;
            $('#title').append("<td>商品类型</td>");
            $('#sum').append("<td>收藏量</td>");
            for (var i = 0; i < lists.length; i++) {
                $('#title').append("<td>" + lists[i].title + "</td>");
                $('#sum').append("<td>" + lists[i].count + "</td>");
                count.push(lists[i].count);
                type.push(lists[i].title);
            }
            echart(type, count, '收藏量');
        }
    });
};
//点击量
hitsSum();

function hitsSum() {
    $.ajax({
        url: '/goodsTree/hitsSum',
        type: 'post',
        dataType: 'json',
        success: function (data) {
            var type = new Array();
            var count = new Array();
            var lists = data.data;
            $('#title').append("<td>商品类型</td>");
            $('#sum').append("<td>点击量</td>");
            for (var i = 0; i < lists.length; i++) {
                $('#title').append("<td>" + lists[i].title + "</td>");
                $('#sum').append("<td>" + lists[i].count + "</td>");
                count.push(lists[i].count);
                type.push(lists[i].title);
            }
            echart(type, count, '点击量');
        }
    });
}
//返回
function goBack() {
    //通过页面查找iframe
    parent.$('#recordLog').val($('#goodsStatistics').text());
    parent.before();
    return false;
}

